package com.zzt.zt_autocompletetextview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zeting
 * @date: 2022/9/26
 */
public class MyAutoAdapter extends ArrayAdapter implements Filterable {
    // 经过过滤的数组
    private ArrayList<Language> languages;
    // 没有经过过滤的数组
    private ArrayList<Language> origin;
    private Context context;
    private int layoutId;

    public MyAutoAdapter(Context context, int resource, ArrayList<Language> languages) {
        super(context, resource);
        this.languages = languages;
        this.origin = languages;
        this.context = context;
        this.layoutId = resource;
    }

    @Override
    public Language getItem(int position) {
        return languages == null ? null : languages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public int getCount() {
        return languages == null ? 0 : languages.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(languages.get(position).name);
        holder.icon.setBackgroundResource(languages.get(position).icon);
        return convertView;
    }

    @Override
    public Filter getFilter() {
        return new MyFilter();
    }

    private class ViewHolder {
        TextView name;
        ImageView icon;
    }

    public class MyFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (null == constraint || 0 == constraint.length()) {
                constraint = "";
            }
            // 这里做一些简单的过滤
            String condition = String.valueOf(constraint).toLowerCase();
            List<Language> temp = new ArrayList<>();
//            for (Language language : origin) {
//                if (language.name.toLowerCase().contains(condition)) {
//                    temp.add(language);
//                }
//            }

            temp.add(new Language(condition + "@163.com"));
            temp.add(new Language(condition + "@gemail.com"));
            temp.add(new Language(condition + "@qq.com"));
            temp.add(new Language(condition + "@hotmail.com"));


            results.values = temp;
            results.count = temp.size();
            // 返回的results会在publishResult()函数中得到
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            languages = (ArrayList<Language>) results.values;
            // 更新视图
            notifyDataSetChanged();
        }
    }
}
